package com.wd.health.view.activity.mine;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tencent.mm.opensdk.modelmsg.SendAuth;

import com.wd.health.R;
import com.wd.health.app.App;
import com.wd.health.model.bean.LoginBean;
import com.wd.health.utils.RetrofitManager;
import com.wd.health.utils.RsaCoder;
import com.gyf.immersionbar.ImmersionBar;
import com.wd.health.utils.Utils;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class LoginActivity extends AppCompatActivity {

    private TextView tv_register;
    private Button btn_login;
    private EditText et_login_email;
    private EditText et_login_pwd;
    private ImageView img_wx;
    private ImageView img_face;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ImmersionBar.with(this).init();
        initView();
        initData();
    }

    private void initData() {
        btn_login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String email = et_login_email.getText().toString();
                String pwd = et_login_pwd.getText().toString();


                try {
                    String mpwd = RsaCoder.encryptByPublicKey(pwd);
                    RetrofitManager.getInstance().postLoginData(email, mpwd, new Observer<LoginBean>() {

                        private String s1;

                        @Override
                        public void onSubscribe(Disposable d) {

                        }

                        @Override
                        public void onNext(LoginBean loginBean) {
                            String userName = loginBean.getResult().getUserName();
                            Log.e("TAG", "userName: "+userName );
                            String jiGuangPwd = loginBean.getResult().getJiGuangPwd();
                            Log.e("TAG", "jiGuangPwd: "+jiGuangPwd );
                            try {
                                String s = RsaCoder.decryptByPublicKey(jiGuangPwd);
                                s1 = Utils.md5(s);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            JMessageClient.login(userName,s1, new BasicCallback() {
                                @Override
                                public void gotResult(int i, String s) {
                                    if (i == 0){
                                        Log.e("TAG", "gotResult:"+"极光登录成功");
                                    }else {
                                        Log.e("TAG", "gotResult:"+"极光登录失败");
                                    }
                                }
                            });
                            if (loginBean.getMessage().equals("登录成功")){
                                String headPic = loginBean.getResult().getHeadPic();
                                String nickName = loginBean.getResult().getNickName();
                                int userId = loginBean.getResult().getUserId();
                                String sessionId = loginBean.getResult().getSessionId();
                                String email1 = loginBean.getResult().getEmail();
                                SharedPreferences user = getSharedPreferences("user", Context.MODE_PRIVATE);
                                user.edit()
                                        .putString("email",email1)
                                        .putString("headPic",headPic)
                                        .putString("nickName",nickName)
                                        .putInt("userId",userId)
                                        .putString("sessionId",sessionId)
                                        .putBoolean("login",true)
                                        .commit();
                                Intent intent = new Intent(LoginActivity.this, MineActivity.class);
                                startActivity(intent);
                                finish();
                            }
                        }

                        @Override
                        public void onError(Throwable e) {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private void initView() {
        tv_register = findViewById(R.id.tv_register);
        btn_login = findViewById(R.id.btn_login);
        et_login_email = findViewById(R.id.et_login_email);
        et_login_pwd = findViewById(R.id.et_login_pwd);
        img_wx = findViewById(R.id.img_wx);
        img_face = findViewById(R.id.img_face);
        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(intent);
                finish();
            }
        });
        img_wx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                wake();
            }
        });
        img_face.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(LoginActivity.this, FaceLoginActivity.class);
                startActivity(intent);
            }
        });
    }

    private void wake() {
        SendAuth.Req req = new SendAuth.Req();
        req.scope = "snsapi_userinfo";
        req.state = "wechat_sdk_demo_test";
        //像微信发送请求
        App.mWxApi.sendReq(req);
        finish();
    }
}